package audio;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;

public class Test2 {
	public static Gson gson=new Gson();
	public static void main(String[] args) throws Exception {
		String tokenStr=requestGet(Consts.token);
		Token token=gson.fromJson(tokenStr, Token.class);
		Map<String,Object>param=new HashMap<String,Object>();
		param.put("tex", "现在时间16:20");
		param.put("tok", token.getAccess_token());
		param.put("cuid", "34-68-95-91-77-43");
		param.put("ctp", "1");
		param.put("lan", "zh");
		param.put("spd", "5");
		param.put("pit", "5");
		param.put("vol", "5");
		param.put("per", "0");
		String pm=convertParam(param);
		String str=requestGet(Consts.compress+"?"+pm);
		//System.out.println(str);
	}
	
	public static String requestGet(String urlStr) throws Exception {
		URL url=new URL(urlStr);
		System.out.println(urlStr);
		HttpURLConnection connect=(HttpURLConnection) url.openConnection();
		InputStream in=connect.getInputStream();
		int len;
		byte[] bb=new byte[1024*2];
		String result="";
		while((len=in.read(bb))>0) {
			result+=new String(bb,0,len);
		}
		return result;
	}
	
	public static String convertParam(Map<String,Object>param) {
		String result="";
		for(String key:param.keySet()) {
			try {
				result+=key+"="+URLEncoder.encode((String)param.get(key),"UTF-8")+"&";
			}catch(Exception e) {
				result=null;
			}
		}
		return result;
	}
}
